<?php
namespace App\Service\Admin;

class DepartService extends BaseService
{
    //数据列表
    public static function index($pas, $pages)
    {
        $start = ($pages['start'] - 1) * $pages['limit'];
        $db = self::dbConn(self::$dbStDepart)->where('del', 0);
//        if ($pas['depart_id']) {
//            $db = $db->where('depart_id', $pas['depart_id']);
//        }
       if ($pas['keyword']) {
           $db = $db->where('title', 'like', '%'.$pas['keyword'].'%');
       }
       if ($pas['sort_id'] == 2) {
           $db = $db->orderBy('uptime', 'desc');
       } else {
           $db = $db->orderBy('uptime', 'asc');
       }
        $total = $db->count();
        $res = $db->skip($start)->take($pages['limit'])->get();
        if (!count($res)) {
            return self::toback(-2, '数据不存在');
        }
        $arys = [];
        foreach ($res as $v) {
            $staffOnes = self::dbConn(self::$dbStaff)
                ->where('depart_id', $v->id)
                ->get();
            $staffList = [];
            if (count($staffOnes)) {
                foreach ($staffOnes as $vv) {
                    $staffList[] = $vv->title;
                }
            }
            $staff_str = $staffList ? implode('，', $staffList) : '';
            $arys[] = [
                'id' => $v->id, 'title' => $v->title,
                'staff_str' => $staff_str,
                'is_able' => $v->is_able,
            ];
        }
        return self::toback(0, '', $arys, $total);
    }

    //新增
    public static function add($pas)
    {
        $res = self::dbConn(self::$dbStDepart)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $pas['addtime'] = $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStDepart)->insertGetId($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //修改
    public static function modify($id, $pas)
    {
        $res = self::dbConn(self::$dbStDepart)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStDepart)
            ->where('del', 0)
            ->where('title', $pas['title'])
            ->where('id', '<>', $id)
            ->first();
        if (!empty($res)) {
            return self::toback(-2, '存在同名数据');
        }
        $pas['uptime'] = toDate();
        $res = self::dbConn(self::$dbStDepart)
            ->where('id', $id)
            ->update($pas);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //删除
    public static function del($id)
    {
        $res = self::dbConn(self::$dbStDepart)->where('id', $id)->first();
        if (empty($res)) {
            return self::toback(-2, '数据不存在');
        }
        $res = self::dbConn(self::$dbStDepart)
            ->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$res) {
            return self::toback(-1, 'error');
        }
        return self::toback();
    }

    //禁用启用
    public static function ableSet($id, $pas)
    {
        $res = self::dbConn(self::$dbStDepart)
            ->where('del', 0)
            ->where('id', $id)
            ->first();
        if (!$res) {
            return self::toback(-2, '没有数据');
        }
        self::dbConn(self::$dbStDepart)
            ->where('id', $id)
            ->update($pas);
        return self::toback();
    }
}